package com.zhuoyue;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuoyue.biz.core.api.dao.TblDeviceActionsMapper;
import com.zhuoyue.biz.core.api.dao.TblDeviceTaskMapper;
import com.zhuoyue.biz.core.api.domain.DeviceDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.api.service.DeviceTaskService;
import com.zhuoyue.biz.core.api.service.GatewayRedisService;
import com.zhuoyue.biz.core.api.service.ZigbeeDevicePropertyService;
import com.zhuoyue.biz.core.api.service.impl.DeviceService;
import com.zhuoyue.biz.core.command.cmd.common.fac.base.ZhiNengKongTiaoMianBanConstant;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private GatewayRedisService gatewayRedisService;

    @Autowired
    private ZigbeeDevicePropertyService zigbeeDevicePropertyService;
    @Autowired
    private TblDeviceActionsMapper actionsMapper;

    @Test
    public void getDeviceTest() throws InvocationTargetException, IllegalAccessException {
        QueryWrapper queryWrapper = new QueryWrapper();
        String gatewayId = "0b95d2b2df992f1ccb4c0303f322dccb";
        queryWrapper.eq("gateway_id", gatewayId);
        queryWrapper.eq("od_index", "0FE6");
        queryWrapper.eq("device_type", "02");
        queryWrapper.eq("product_type", "02");
        queryWrapper.or();
        queryWrapper.eq("gateway_id", gatewayId);
        queryWrapper.eq("od_index", "0FE6");
        queryWrapper.eq("device_type", "02");
        queryWrapper.eq("product_type", "07");

        List<DeviceDO> deviceDOS = deviceService.list(queryWrapper);
    }

    @Autowired
    TblDeviceTaskMapper taskMapper;
    @Autowired
    DeviceTaskService deviceTaskService;
    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void test3() {
//        Integer count = taskMapper.selectCount(Wrappers.<TblDeviceTask>lambdaQuery().eq(TblDeviceTask::getZigbeeMac, "28EBBF0A006F0D00"));
//        System.out.println("count: " + count);
//        TblDeviceTask deviceTask = new TblDeviceTask();
//        deviceTask.setZigbeeMac("462dac5e283263b24eea81b3474557aa");
//        deviceTask.setTaskIndex(2);
//        deviceTask.setStatus(0);
//        deviceTask.setStartTime(LocalTime.of(5, 30));
//        deviceTask.setTaskInterval(30);
//        deviceTask.setControlType(1);
//        deviceTask.setControlParams("0102FFFFFFFF");
//        deviceTaskService.addOrUpdateDeviceTask(deviceTask);
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        redisTemplate.setValueSerializer(stringRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashValueSerializer(stringRedisSerializer);
//
//        redisTemplate.opsForValue().set("key", "value", 100, TimeUnit.SECONDS);
//        Object object = redisTemplate.opsForValue().get("set_device_task_zigbeeMac_462dac5e283263b24eea81b3474557aa_index_2");
//        if (object != null) {
//            TblDeviceTask deviceTask1 = (TblDeviceTask) object;
//            System.out.println("value: " + deviceTask1);
//        } else {
//            System.out.println("value: " + redisTemplate.opsForValue().get("key"));
//        }

        deviceTaskService.addOrUpdateDeviceTask("28EBBF0A006F0D00", 1);
    }

    /**
     * 测试联控面板100次开关
     */
    @Test
    public void test0FAA0504() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("18BEE80D006F0D00", "2A1C020118BEE80D006F0D00F79CBA10006F0D000FAAFF060001800001016423");
            } else {
                gatewayRedisService.writeDataToGateway("18BEE80D006F0D00", "2A1C020118BEE80D006F0D00F79CBA10006F0D000FAAFF060001800001026523");
            }
        }
    }


    @Test
    public void test0FAA0604_1() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("D0DCCCFEFF142E84", "2A1C0201D0DCCCFEFF142E84766E720E006F0D000FAAFF060001800001015F23");
            } else {
                gatewayRedisService.writeDataToGateway("D0DCCCFEFF142E84", "2A1C0201D0DCCCFEFF142E84766E720E006F0D000FAAFF060001800001026023");
            }
        }
    }

    @Test
    public void test0FAA0604_2() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("D0DCCCFEFF142E84", "2A1C0201D0DCCCFEFF142E84766E720E006F0D000FAAFF06000C00000101EA23");
            } else {
                gatewayRedisService.writeDataToGateway("D0DCCCFEFF142E84", "2A1C0201D0DCCCFEFF142E84766E720E006F0D000FAAFF06000C00000102EB23");
            }
        }
    }


    /**
     * 三路联控面板
     *
     * @throws InterruptedException
     */
    @Test
    public void test0FAA0704_1() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF060001800001010723");
            } else {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF060001800001020823");
            }
        }
    }

    @Test
    public void test0FAA0704_2() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06000C000001019223");
            } else {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06000C000001029323");
            }
        }
    }

    @Test
    public void test0FAA0704_3() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06006000000101E623");
            } else {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06006000000102E723");
            }
        }
    }


    @Test
    public void test0FAA0604_3() throws InterruptedException {
        int count = 300;
        for (int i = 0; i < count; i++) {
            Thread.sleep(500);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06006000000101E623");
            } else {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1C02013FE6CCFEFF142E84E5F2AC10006F0D000FAAFF06006000000102E723");
            }
        }
    }


    /**
     * 网关
     *
     * @throws InterruptedException
     */
    @Test
    public void gateway_3FE6CCFEFF142E84() throws InterruptedException {
        int count = 100;
        for (int i = 0; i < count; i++) {
            Thread.sleep(3000);
            System.out.println("i = " + i + "次");
            if (i % 2 != 0) {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1160003FE6CCFEFF142E8406010101F000000D23");
            } else {
                gatewayRedisService.writeDataToGateway("3FE6CCFEFF142E84", "2A1160003FE6CCFEFF142E84060101010000001D23");
            }
        }
    }


    /**
     * 添加大量设备
     *
     * @throws InterruptedException
     */
    @Test
    public void addBigDevice() throws InterruptedException {
        int count = 500;
        List<DeviceDO> deviceDOS = new ArrayList<>(500);
        List<TblDeviceActions> deviceActionList = new ArrayList<>(1000);
        for (int i = 0; i < count; i++) {
            DeviceDO deviceDO = new DeviceDO();
            deviceDO.setGatewayId("6c4562f7854609c70dfa2b044bf4a070");
            deviceDO.setName("一路开关" + i);
            deviceDO.setImgUrl("http://www.img.zyzncloud.com/zyzn/devices/0faa/0faa0504_u.png");
            deviceDO.setOdIndex("0FAA");
            deviceDO.setDeviceType("05");
            deviceDO.setProductType("04");
            deviceDO.setZigbeeMac("11111111116F0D" + i);
            deviceDO.setType(1);
            if (i % 2 != 0) {
                deviceDO.setSwitchState("01");
            } else {
                deviceDO.setSwitchState("02");
            }
            deviceDO.setSwitchTime(LocalDateTime.now());
            deviceDO.setPushType("1");
            deviceDO.setSpreadingType("00");
            deviceDOS.add(deviceDO);

            TblDeviceActions tblDeviceActions1 = new TblDeviceActions();
            tblDeviceActions1.setDeviceId(deviceDO.getId());
            tblDeviceActions1.setActionsKey("switchState");
            tblDeviceActions1.setActionsValue(deviceDO.getSwitchState());

            TblDeviceActions tblDeviceActions2 = new TblDeviceActions();
            tblDeviceActions2.setDeviceId(deviceDO.getId());
            tblDeviceActions2.setActionsKey("controlType");
            tblDeviceActions2.setActionsValue("DIRECT_CONTROL");

            deviceActionList.add(tblDeviceActions1);
            deviceActionList.add(tblDeviceActions2);
        }
        boolean b = deviceService.saveBatch(deviceDOS);
        if (b) {
            Thread.sleep(1000);
            for (int i = 0; i < deviceActionList.size(); i++) {
                actionsMapper.insert(deviceActionList.get(i));
            }
        }

    }

    @Test
    public void test1() {
//        Object deviceList = redisTemplate.opsForValue().get("D063D7FEFF142E84_CE5FBCFEFFBD1BEC");
//        if (deviceList == null) {
//            System.out.println("deviceList is null");
//        } else {
//            System.out.println("deviceList: " + deviceList.toString());
//        }

        redisTemplate.opsForValue().set("123456", "redis test");
        String redisTest = (String) redisTemplate.opsForValue().get("123456");
        if (redisTest == null) {
            System.out.println("redisTest is null");
        } else {
            System.out.println("redisTest: " + redisTest);
        }
    }

    @Test
    public void test2() {
        String modeValue = "03";
        String nameByValue = ZhiNengKongTiaoMianBanConstant.AirConditionModeEnum.getNameByValue(modeValue);
        System.out.println("nameByValue: " + nameByValue);
    }
}
